package eu.ahsx.limboa.utils
{
	/**
	 * 	Utility class allowing to easily create token
	 * 
	 * 	@author Masy Alexandre
	 * */
	public class Token
	{
		/**
		 * 	Generate a token based on a pattern
		 * 	
		 * 	@param	length int length of the token
		 * 	@param	pattern String String pattern of allowed caracters
		 * 	@return String
		 * */
		public static function generate(length:int, pattern:String="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"):String
		{
			var ret:String = "";
			var n:int = 0;
			var mn:int = pattern.length;
			while(--length > -1)
			{
				n = Math.random()*mn;
				ret += pattern.charAt(n);
			}
			
			return ret;
		}
		
		/**
		 * 	Generate a uniq token.
		 * 
		 * 	@param	length int Length of the token	
		 * 	@return String return an alphanumeric string with the uniq token
		 * */
		public static function uniq(length:int = 10):String
		{
			var ret:String = "";
			var time:int = new Date().time;
			
			Random.seed = time*1000000;
			var pattern:String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
			var nm:int = pattern.length;
			while(--length > -1)
				ret += pattern.charAt( (Random.random()%nm) * nm );

			return ret;
		}
	}
}